<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>

</body>
<script>
    // 构造函数  String()  Number()  Boolean()  Array()  Object()


    // 1. 作为普通函数调用
    // console.log(String(100));
    // console.log(String("100"));
    // console.log(String(true));

    // console.log(Number(100));
    // console.log(Number("100"));
    // console.log(Number(true));

    // console.log(Boolean(100));
    // console.log(Boolean("100"));
    // console.log(Boolean(true));

    // console.log(Array(10));  //作为普通函数调用  new Array(10)
    // console.log(Array(10, 20, 30, 40));  //作为普通函数调用  new Array(10)


    // 作为构造函数调用
    // console.log(new String(100));
    // console.log(new String("100"));
    // console.log(new String(true));

    // console.log(new Number(100));
    // console.log(new Number("100"));
    // console.log(new Number(true));

    // console.log(new Boolean(100));
    // console.log(new Boolean("100"));
    // console.log(new Boolean(true));

    // console.log(new Array(10));  //作为普通函数调用  new Array(10)
    // console.log(new Array(10, 20, 30, 40));  //作为普通函数调用  new Array(10)
</script>
<script>
    function Point(x, y) {
        // 作为构造函数调用 new Point() => 创建实例化对象 => 实例化对象都有constructor指向构造函数本身

        // {}
        // {}[[Prototype]] = Point.prototype;
        // this -> {}

        // Object.getPrototypeOf(this) === Point.prototype
        if (this.constructor == Point) {
            this.x = x;
            this.y = y;
        } else if (this === window) { // 作为普通函数调用
            return new Point(x, y);
        } else {
            this.x = x;
            this.y = y;
        }
        // return this;
    }

    // prototype => 原型对象 =>存放所有实例化对象共有的属性和方法
    // Point.prototype = {constructor:Point}

    Point.prototype.toString = function () { // (x,y)
        // this -> 调用此方法的实例化对象
        return `(${this.x},${this.y})`;
    }

    // 作为普通函数调用
    var result = Point(10, 20);
    console.log(result);

    // 作为构造函数调用   
    // var p1 = new Point(10, 20); // {x:10,y:20}
    // console.log(p1);

</script>

</html>